
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using System.Linq.Expressions;
using Apps.IDAL.PL;
using Apps.Models.PL;
using Apps.IBLL.PL;
namespace Apps.BLL.PL
{
    public partial class PL_RC_DETAILSBLL : Virtual_PL_RC_DETAILSBLL, IPL_RC_DETAILSBLL
    {
        public virtual bool IsUnloading(object id)
        {
            using (DBContainer db = new DBContainer())
            {
                List<PL_RC_DETAILSModel> list = GetList(a => a.PLAN_ID == id.ToString());
                string detailId = string.Join(",", list.Select(t => t.DETAIL_ID).ToArray());
                List<V_PL_RC_UPLOADING> q1 = db.V_PL_RC_UPLOADING.Where(a => detailId.Contains(a.DETAIL_ID) && a.STOCKPILE_ID != null).ToList();
                if (q1.Count <= 0 || list.Count != q1.Count)
                {
                    return false;
                }
                return true;
            }
        }
    }
	public class Virtual_PL_RC_DETAILSBLL
	{
        [Dependency]
        public IPL_RC_DETAILSRepository m_Rep { get; set; }

		public virtual List<PL_RC_DETAILSModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<PL_RC_DETAILS> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
								a=>a.BEGIN_TIME.Contains(queryStr)
								|| a.END_TIME.Contains(queryStr)
                                || a.REMARKS.Contains(queryStr)
								);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
		 
		public virtual List<PL_RC_DETAILSModel> GetPagerList(ref GridPager pager, Expression<Func<PL_RC_DETAILS, bool>> whereLambda )
        {

            IQueryable<PL_RC_DETAILS> queryData = m_Rep.GetList(whereLambda); 
             
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

		public virtual List<PL_RC_DETAILSModel> GetList(Expression<Func<PL_RC_DETAILS, bool>> whereLambda )
        {

            IQueryable<PL_RC_DETAILS> queryData = m_Rep.GetList(whereLambda); 
       
            return CreateModelList(ref queryData);
        }

        public virtual List<PL_RC_DETAILSModel> GetListByParentId(ref GridPager pager, string queryStr, object parentId)
        {
            return new List<PL_RC_DETAILSModel>();
        }

        public virtual List<PL_RC_DETAILSModel> CreateModelList(ref IQueryable<PL_RC_DETAILS> queryData)
        {

            List<PL_RC_DETAILSModel> modelList = (from r in queryData
                                              select new PL_RC_DETAILSModel
                                              {
													DETAIL_ID = r.DETAIL_ID,
													PLAN_ID = r.PLAN_ID,
													BATCH_CODE = r.BATCH_CODE,
													AMOUNT = r.AMOUNT,
													QNET = r.QNET,
													ST = r.ST,
													PLAN_DATE = r.PLAN_DATE,
													BEGIN_TIME = r.BEGIN_TIME,
													END_TIME = r.END_TIME,
													MATERIEL_ID = r.MATERIEL_ID,
													SUPPLIER_ID = r.SUPPLIER_ID,
													MINE_ID = r.MINE_ID,
													TRANSPORT_ID = r.TRANSPORT_ID,
													REMARKS = r.REMARKS,
													CONTRACT_DETAIL_ID = r.CONTRACT_DETAIL_ID,
          
                                              }).ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, PL_RC_DETAILSModel model)
        {
            try
            {
                PL_RC_DETAILS entity = m_Rep.GetById(model.DETAIL_ID);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new PL_RC_DETAILS();
               	entity.DETAIL_ID = model.DETAIL_ID;
				entity.PLAN_ID = model.PLAN_ID;
				entity.BATCH_CODE = model.BATCH_CODE;
				entity.AMOUNT = model.AMOUNT;
				entity.QNET = model.QNET;
				entity.ST = model.ST;
				entity.PLAN_DATE = model.PLAN_DATE;
				entity.BEGIN_TIME = model.BEGIN_TIME;
				entity.END_TIME = model.END_TIME;
				entity.MATERIEL_ID = model.MATERIEL_ID;
				entity.SUPPLIER_ID = model.SUPPLIER_ID;
				entity.MINE_ID = model.MINE_ID;
				entity.TRANSPORT_ID = model.TRANSPORT_ID;
				entity.REMARKS = model.REMARKS;
				entity.CONTRACT_DETAIL_ID = model.CONTRACT_DETAIL_ID;
  

                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
		 public virtual bool Deletes(ref ValidationErrors errors,Expression<Func<PL_RC_DETAILS, bool>> whereLambda)
         {
		        try
				{
					using (DBContainer db = new DBContainer())
					{
				
							foreach (var entity in db.PL_RC_DETAILS.Where(whereLambda))
							{
								db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
							}

							if(db.SaveChanges()>0)
							{
								return true;
							}
							else
							{
								return false;
							}
			
					}
				}
				catch (Exception ex)
				{
					errors.Add(ex.Message);
					ExceptionHander.WriteException(ex);
					return false;
				}
        }

         public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
       

        public virtual bool Edit(ref ValidationErrors errors, PL_RC_DETAILSModel model)
        {
            try
            {
                PL_RC_DETAILS entity = m_Rep.GetById(model.DETAIL_ID);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                              				entity.DETAIL_ID = model.DETAIL_ID;
				entity.PLAN_ID = model.PLAN_ID;
				entity.BATCH_CODE = model.BATCH_CODE;
				entity.AMOUNT = model.AMOUNT;
				entity.QNET = model.QNET;
				entity.ST = model.ST;
				entity.PLAN_DATE = model.PLAN_DATE;
				entity.BEGIN_TIME = model.BEGIN_TIME;
				entity.END_TIME = model.END_TIME;
				entity.MATERIEL_ID = model.MATERIEL_ID;
				entity.SUPPLIER_ID = model.SUPPLIER_ID;
				entity.MINE_ID = model.MINE_ID;
				entity.TRANSPORT_ID = model.TRANSPORT_ID;
				entity.REMARKS = model.REMARKS;
				entity.CONTRACT_DETAIL_ID = model.CONTRACT_DETAIL_ID;
 


                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

      

        public virtual PL_RC_DETAILSModel GetById(object id)
        {
            if (IsExists(id))
            {
                PL_RC_DETAILS entity = m_Rep.GetById(id);
                PL_RC_DETAILSModel model = new PL_RC_DETAILSModel();
                              				model.DETAIL_ID = entity.DETAIL_ID;
				model.PLAN_ID = entity.PLAN_ID;
				model.BATCH_CODE = entity.BATCH_CODE;
				model.AMOUNT = entity.AMOUNT;
				model.QNET = entity.QNET;
				model.ST = entity.ST;
				model.PLAN_DATE = entity.PLAN_DATE;
				model.BEGIN_TIME = entity.BEGIN_TIME;
				model.END_TIME = entity.END_TIME;
				model.MATERIEL_ID = entity.MATERIEL_ID;
				model.SUPPLIER_ID = entity.SUPPLIER_ID;
				model.MINE_ID = entity.MINE_ID;
				model.TRANSPORT_ID = entity.TRANSPORT_ID;
				model.REMARKS = entity.REMARKS;
				model.CONTRACT_DETAIL_ID = entity.CONTRACT_DETAIL_ID;
 
                return model;
            }
            else
            {
                return null;
            }
        }

		public virtual PL_RC_DETAILSModel GetItem(Expression<Func<PL_RC_DETAILS, bool>> whereLambda)
        {
 
                PL_RC_DETAILS entity = m_Rep.GetItem(whereLambda);
                if (entity == null) return null;

                PL_RC_DETAILSModel model = new PL_RC_DETAILSModel();
                              				model.DETAIL_ID = entity.DETAIL_ID;
				model.PLAN_ID = entity.PLAN_ID;
				model.BATCH_CODE = entity.BATCH_CODE;
				model.AMOUNT = entity.AMOUNT;
				model.QNET = entity.QNET;
				model.ST = entity.ST;
				model.PLAN_DATE = entity.PLAN_DATE;
				model.BEGIN_TIME = entity.BEGIN_TIME;
				model.END_TIME = entity.END_TIME;
				model.MATERIEL_ID = entity.MATERIEL_ID;
				model.SUPPLIER_ID = entity.SUPPLIER_ID;
				model.MINE_ID = entity.MINE_ID;
				model.TRANSPORT_ID = entity.TRANSPORT_ID;
				model.REMARKS = entity.REMARKS;
				model.CONTRACT_DETAIL_ID = entity.CONTRACT_DETAIL_ID;
 
                return model; 
        }


		 /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<PL_RC_DETAILSModel> list,ref ValidationErrors errors )
        {
          
            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
			 				 excelFile.AddMapping<PL_RC_DETAILSModel>(x => x.DETAIL_ID, "DETAIL_ID");
				 excelFile.AddMapping<PL_RC_DETAILSModel>(x => x.PLAN_ID, "PLAN_ID");
				 excelFile.AddMapping<PL_RC_DETAILSModel>(x => x.BATCH_CODE, "BATCH_CODE");
				 excelFile.AddMapping<PL_RC_DETAILSModel>(x => x.AMOUNT, "AMOUNT");
				 excelFile.AddMapping<PL_RC_DETAILSModel>(x => x.QNET, "QNET");
				 excelFile.AddMapping<PL_RC_DETAILSModel>(x => x.ST, "ST");
				 excelFile.AddMapping<PL_RC_DETAILSModel>(x => x.PLAN_DATE, "PLAN_DATE");
				 excelFile.AddMapping<PL_RC_DETAILSModel>(x => x.BEGIN_TIME, "BEGIN_TIME");
				 excelFile.AddMapping<PL_RC_DETAILSModel>(x => x.END_TIME, "END_TIME");
				 excelFile.AddMapping<PL_RC_DETAILSModel>(x => x.MATERIEL_ID, "MATERIEL_ID");
				 excelFile.AddMapping<PL_RC_DETAILSModel>(x => x.SUPPLIER_ID, "SUPPLIER_ID");
				 excelFile.AddMapping<PL_RC_DETAILSModel>(x => x.MINE_ID, "MINE_ID");
				 excelFile.AddMapping<PL_RC_DETAILSModel>(x => x.TRANSPORT_ID, "TRANSPORT_ID");
				 excelFile.AddMapping<PL_RC_DETAILSModel>(x => x.REMARKS, "REMARKS");
				 excelFile.AddMapping<PL_RC_DETAILSModel>(x => x.CONTRACT_DETAIL_ID, "CONTRACT_DETAIL_ID");
 
            //SheetName
            var excelContent = excelFile.Worksheet<PL_RC_DETAILSModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new PL_RC_DETAILSModel();
						 				  entity.DETAIL_ID = row.DETAIL_ID;
				  entity.PLAN_ID = row.PLAN_ID;
				  entity.BATCH_CODE = row.BATCH_CODE;
				  entity.AMOUNT = row.AMOUNT;
				  entity.QNET = row.QNET;
				  entity.ST = row.ST;
				  entity.PLAN_DATE = row.PLAN_DATE;
				  entity.BEGIN_TIME = row.BEGIN_TIME;
				  entity.END_TIME = row.END_TIME;
				  entity.MATERIEL_ID = row.MATERIEL_ID;
				  entity.SUPPLIER_ID = row.SUPPLIER_ID;
				  entity.MINE_ID = row.MINE_ID;
				  entity.TRANSPORT_ID = row.TRANSPORT_ID;
				  entity.REMARKS = row.REMARKS;
				  entity.CONTRACT_DETAIL_ID = row.CONTRACT_DETAIL_ID;
 
                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<PL_RC_DETAILSModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        PL_RC_DETAILS entity = new PL_RC_DETAILS();
                       						entity.DETAIL_ID = model.DETAIL_ID;
						entity.PLAN_ID = model.PLAN_ID;
						entity.BATCH_CODE = model.BATCH_CODE;
						entity.AMOUNT = model.AMOUNT;
						entity.QNET = model.QNET;
						entity.ST = model.ST;
						entity.PLAN_DATE = model.PLAN_DATE;
						entity.BEGIN_TIME = model.BEGIN_TIME;
						entity.END_TIME = model.END_TIME;
						entity.MATERIEL_ID = model.MATERIEL_ID;
						entity.SUPPLIER_ID = model.SUPPLIER_ID;
						entity.MINE_ID = model.MINE_ID;
						entity.TRANSPORT_ID = model.TRANSPORT_ID;
						entity.REMARKS = model.REMARKS;
						entity.CONTRACT_DETAIL_ID = model.CONTRACT_DETAIL_ID;
 
                        db.PL_RC_DETAILS.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
		  public void Dispose()
        { 
            
        }

	}
}
